home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / vbof_v11 / vbofemgr.cls < prev    next >
Text File  |  1996-03-01  |  13KB  |  383 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "VBOFEventManager"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' (c) Copyright 1995 Ken Fitzpatrick
  11. '     All Rights Reserved
  12. '     Cannot be distributed or sold without permission
  13. '
  14. ' VBObjectFrameworkEventManager is a supplemental
  15. '   Event Manager for Microsoft Visual Basic 4.0.
  16. '   It is valid only in conjunction with the
  17. '   following Classes Modules:
  18. '       VBOFCollection
  19. '       VBOFObjectLink
  20. '       VBOFObjectManager
  21. '       VBOFEventObject
  22. '
  23. ' The VBOFEventManager interface, while
  24. '   public, is not for public use.
  25. '   VBOFEventManager is fully
  26. '   encapsulated by VBOFObjectManager
  27. '   and applications should use that interface
  28. '   for all VBOFEventManager activity.
  29. '
  30. ' See Class Module "VBOFObjectManager" for
  31. '     documentation details
  32.  
  33. Private pvtObjectEvents As New Collection
  34. Private pvtCollectionEvents As New Collection
  35. Private pvtVBOFObjectManager As VBOFObjectManager
  36.  
  37. Private Const pvtReceiverDoesNotSupportThisMethod = 438
  38.  
  39. Public Property Get ObjectManager() As VBOFObjectManager
  40. Attribute ObjectManager.VB_Description = "Private"
  41.     Set ObjectManager = pvtVBOFObjectManager
  42. End Property
  43.  
  44.  
  45. Public Property Set ObjectManager(anObjectManager As VBOFObjectManager)
  46.     Set pvtVBOFObjectManager = anObjectManager
  47. End Property
  48.  
  49.  
  50.  
  51. Public Function ObjectID() As Long
  52. Attribute ObjectID.VB_Description = "Private"
  53.     ObjectID = -1
  54. End Function
  55.  
  56.  
  57.  
  58. Public Function RegisterForObjectEvent(Optional TriggerObject As Variant, Optional TriggerObjectType As Variant, Optional TriggerEvent As Variant, Optional RegisterObject As Variant, Optional RegisterType As Variant, Optional SkipTriggerObject As Variant, Optional NewEventObject As Variant) As Boolean
  59. Attribute RegisterForObjectEvent.VB_Description = "Private"
  60. ' Register the specified RegisterObject or
  61. '   RegisterType to receive notification upon the
  62. '   posting of the specified TriggerEvent by the
  63. '   specified TriggerObject or TriggerObjectType
  64.  
  65. #If NoEventMgr = False Then
  66.     Dim tempNewEventObject As New VBOFEventObject
  67.     Dim tempEventObject As VBOFEventObject
  68.     
  69.     On Local Error Resume Next
  70.     
  71. ' bullet-proofing
  72.     If IsMissing(RegisterObject) And IsMissing(RegisterType) Then
  73.         pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForObjectEvent' method for this object because the 'RegisterObject:=' and the 'RegisterType:=' parameters are missing.  At least one of these must be specified."
  74.         RegisterForObjectEvent = False
  75.         Exit Function
  76.     End If
  77.     If IsMissing(TriggerObject) And IsMissing(TriggerObjectType) Then
  78.         If IsMissing(SkipTriggerObject) Or Not SkipTriggerObject Then
  79.             pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForObjectEvent' method for this object because the 'TriggerObject:=' and the 'TriggerObjectType:=' parameters are missing.  At least one of these must be specified."
  80.             RegisterForObjectEvent = False
  81.             Exit Function
  82.         End If
  83.     End If
  84.  
  85. ' initialize the EventObject
  86.     If IsMissing(NewEventObject) Then
  87.         Set tempEventObject = tempNewEventObject
  88.     Else
  89.         Set tempEventObject = NewEventObject
  90.     End If
  91.     Set tempEventObject.ObjectManager = pvtVBOFObjectManager
  92.  
  93.     If Not IsMissing(RegisterObject) Then
  94.         Set tempEventObject.RegisterObject = _
  95.             RegisterObject
  96.     End If
  97.  
  98.     If Not IsMissing(RegisterType) Then
  99.         tempEventObject.RegisterType = _
  100.             RegisterType
  101.     End If
  102.  
  103.     If Not IsMissing(TriggerObject) Then
  104.         Set tempEventObject.TriggerObject = _
  105.             TriggerObject
  106.     End If
  107.  
  108.     If Not IsMissing(TriggerObjectType) Then
  109.         tempEventObject.TriggerObjectType = _
  110.             TriggerObjectType
  111.     End If
  112.     
  113.     If Not IsMissing(TriggerEvent) Then
  114.         tempEventObject.TriggerEvent = _
  115.             TriggerEvent
  116.     End If
  117.     
  118. ' for internal re-use
  119.     If Not IsMissing(SkipTriggerObject) Then
  120.         If SkipTriggerObject = True Then    ' doesn't work when And-ed to above line
  121.             Exit Function
  122.         End If
  123.     End If
  124.  
  125.     pvtObjectEvents.Add _
  126.         tempEventObject
  127. #End If
  128.  
  129.     RegisterForObjectEvent = True
  130. End Function
  131.  
  132. Public Function RegisterForCollectionEvent(Optional Collection As Variant, Optional TriggerObject As Variant, Optional TriggerObjectType As Variant, Optional TriggerEvent As Variant, Optional RegisterObject As Variant, Optional RegisterType As Variant) As Boolean
  133. Attribute RegisterForCollectionEvent.VB_Description = "Private"
  134. ' Register the specified RegisterObject or
  135. '   RegisterType to receive notification upon the
  136. '   posting of the specified TriggerEvent by the
  137. '   specified TriggerObject or TriggerObjectType
  138.  
  139. #If NoEventMgr = False Then
  140.     Dim tempNewEventObject As New VBOFEventObject
  141.     
  142.     On Local Error Resume Next
  143.     
  144. ' bullet-proofing
  145.     If IsMissing(RegisterObject) And IsMissing(RegisterType) Then
  146.         pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForCollectionEvent' method for this object because the 'RegisterObject:=' and the 'RegisterType:=' parameters are missing.  At least one of these must be specified."
  147.         RegisterForCollectionEvent = False
  148.         Exit Function
  149.     End If
  150.  
  151. ' internal re-use
  152.     RegisterForObjectEvent _
  153.         TriggerObject:=TriggerObject, _
  154.         TriggerObjectType:=TriggerObjectType, _
  155.         TriggerEvent:=TriggerEvent, _
  156.         RegisterObject:=RegisterObject, _
  157.         RegisterType:=RegisterType, _
  158.         NewEventObject:=tempNewEventObject, _
  159.         SkipTriggerObject:=True
  160.  
  161.     If Not IsMissing(Collection) Then
  162.         Set tempNewEventObject.Collection = _
  163.             Collection
  164.     End If
  165.  
  166.     pvtCollectionEvents.Add _
  167.         tempNewEventObject
  168. #End If
  169.  
  170.     RegisterForCollectionEvent = True
  171. End Function
  172.  
  173. Public Function TriggerObjectEvent(Optional Event As Variant, Optional Object As Variant, Optional Verbose As Variant) As Boolean
  174. Attribute TriggerObjectEvent.VB_Description = "Private"
  175. ' Trigger the specified Object Event.
  176. ' Post ObjectEventCallBack messages to each of the
  177. '   registered objects
  178.  
  179. #If NoEventMgr = False Then
  180.     Dim tempEventObject As Object
  181.     Dim tempUCaseEvent As String
  182.     
  183.     On Local Error Resume Next
  184.     
  185.     TriggerObjectEvent = True
  186.     tempUCaseEvent = UCase$(Event)
  187.  
  188. ' check for the "Instantated" event, which should
  189. '   be directed towards the new object
  190.     If tempUCaseEvent = "INSTANTIATED" Then
  191.         Object.ObjectEventCallBack _
  192.             Event:=Event, _
  193.             Object:=Object
  194.         Exit Function
  195.     End If
  196.     
  197. ' process each of the EventObjects
  198.     For Each tempEventObject In pvtObjectEvents
  199.  
  200. ' if the event pertains to this EventObject
  201.         If tempEventObject.IsRegisteredForEvent( _
  202.             Event:=Event, _
  203.             Object:=Object) _
  204.         Then
  205.         
  206. ' notify the registered object.
  207.             tempEventObject.RegisterObject.ObjectEventCallBack _
  208.                 Event:=Event, _
  209.                 Object:=Object
  210.  
  211. ' notify of the "missing method" condition
  212.             If Err = pvtReceiverDoesNotSupportThisMethod Then
  213.                 If Not IsMissing(Verbose) Then
  214.                     If Verbose <> False Then
  215.                         pvtErrorMessage "Class Module '" & TypeName(tempEventObject.RegisterObject) & "' does not support the method 'ObjectEventCallBack'." & vbCrLf & "Object cannot be supported by " & TypeName(Me) & " without this method." & vbCrLf & "ObjectType=" & TypeName(tempEventObject.RegisterObject) & ", ObjectID=" & tempEventObject.RegisterObject.ObjectID
  216.                     End If
  217.                 End If
  218.             End If
  219.         End If
  220.     
  221.     Next tempEventObject
  222.  
  223.     Set tempEventObject = Nothing
  224. #End If
  225. End Function
  226.  
  227. Public Function TriggerCollectionEvent(Optional Event As Variant, Optional Object As Variant, Optional Collection As Variant, Optional Verbose As Variant, Optional NoDelete As Variant) As Boolean
  228. Attribute TriggerCollectionEvent.VB_Description = "Private"
  229. ' Trigger the specified Collection Event.
  230. ' Post ObjectEventCallBack messages to each of the
  231. '   registered Collections
  232.  
  233. #If NoEventMgr = False Then
  234.     Dim tempEventObject As Object
  235.     Dim tempNoDelete As Boolean
  236.     Dim I As Long
  237.     
  238.     On Local Error Resume Next
  239.  
  240. ' bullet-proofing
  241. '>>    If TypeName(Object) = "Error" Then
  242. '        Exit Function
  243. '    End If
  244.     tempNoDelete = False
  245.     If Not IsMissing(NoDelete) Then
  246.         tempNoDelete = NoDelete
  247.     End If
  248.  
  249. ' process each of the EventObjects
  250.     I = 1
  251.     For Each tempEventObject In pvtCollectionEvents
  252.  
  253. ' if the event pertains to this EventObject
  254.         If tempEventObject.IsRegisteredForEvent( _
  255.             Event:=Event, _
  256.             Object:=Object, _
  257.             Collection:=Collection, _
  258.             IsCollectionEvent:=True _
  259.         ) Then
  260.         
  261. ' notify the registered object.
  262.             tempEventObject.RegisterObject.ObjectEventCallBack _
  263.                 Event:=Event, _
  264.                 Object:=Object, _
  265.                 NoDelete:=tempNoDelete
  266.  
  267. ' notify of the "missing method" condition
  268.             If Err = pvtReceiverDoesNotSupportThisMethod Then
  269.                 If Not IsMissing(Verbose) Then
  270.                     If Verbose <> False Then
  271.                         pvtErrorMessage "Class Module '" & TypeName(tempEventObject.RegisterObject) & "' does not support the method 'ObjectEventCallBack'." & vbCrLf & "Object cannot be supported by " & TypeName(Me) & " without this method." & vbCrLf & "ObjectType=" & TypeName(tempEventObject.RegisterObject) & ", ObjectID=" & tempEventObject.RegisterObject.ObjectID
  272.                     End If
  273.                 End If
  274.             End If
  275.         End If
  276.     
  277.         I = I + 1
  278.     Next tempEventObject
  279.  
  280.     Set tempEventObject = Nothing
  281. #End If
  282. End Function
  283.  
  284.  
  285.  
  286. Private Function pvtErrorMessage(Optional ErrorMessage As Variant) As Long
  287.     pvtErrorMessage = _
  288.         pvtVBOFObjectManager.DisplayErrorMessage _
  289.             (ErrorMessage)
  290. End Function
  291.  
  292.  
  293. Public Function UnRegisterForCollectionEvent(Optional RegisterObject As Variant, Optional CleanUpMode As Variant) As Boolean
  294. Attribute UnRegisterForCollectionEvent.VB_Description = "Private"
  295. ' UnRegister the specified RegisterObject
  296.  
  297.     Dim tempEventObject As Object
  298.     Dim tempObjectID As Long
  299.     Dim I As Long
  300.     
  301.     On Local Error Resume Next
  302.  
  303.     If Not IsMissing(CleanUpMode) Then
  304.         If CleanUpMode Then
  305.             UnRegisterForCollectionEvent = True
  306.             Exit Function
  307.         End If
  308.     End If
  309.     
  310.     I = 1
  311.     For Each tempEventObject In pvtCollectionEvents
  312.         
  313. ' if the RegisterObject doesn't have an ObjectID,
  314. '   assume it's a Form and force the UnRegister to continue
  315.         tempObjectID = tempEventObject.RegisterObject.ObjectID
  316.         If Err = pvtReceiverDoesNotSupportThisMethod Then
  317.             tempObjectID = RegisterObject.ObjectID
  318.         End If
  319.         
  320.         If tempObjectID = RegisterObject.ObjectID Then
  321.             If TypeName(tempEventObject.RegisterObject) = TypeName(RegisterObject) Then
  322.     
  323.                 pvtCollectionEvents.Remove I
  324.                 Set tempEventObject = Nothing
  325.                 I = I - 1
  326.             End If
  327.         End If
  328.         
  329.         I = I + 1
  330.     Next tempEventObject
  331.  
  332.     UnRegisterForCollectionEvent = True
  333.     Set tempEventObject = Nothing
  334. End Function
  335.  
  336.  
  337. Public Function UnRegisterForObjectEvent(Optional RegisterObject As Variant, Optional CleanUpMode As Variant) As Boolean
  338. Attribute UnRegisterForObjectEvent.VB_Description = "Private"
  339. ' UnRegister the specified RegisterObject
  340.  
  341.     Dim tempEventObject As Object
  342.     Dim tempObjectID As Long
  343.     Dim I As Long
  344.     
  345.     On Local Error Resume Next
  346.     
  347. ' don't bother doing this during "CleanUpMode"
  348.     If Not IsMissing(CleanUpMode) Then
  349.         If CleanUpMode Then
  350.             UnRegisterForObjectEvent = True
  351.             Exit Function
  352.         End If
  353.     End If
  354.  
  355.     I = 1
  356.     For Each tempEventObject In pvtObjectEvents
  357.         
  358. ' if the RegisterObject doesn't have an ObjectID,
  359. '   assume it's a Form and force the UnRegister to continue
  360.         tempObjectID = tempEventObject.RegisterObject.ObjectID
  361.         If Err = pvtReceiverDoesNotSupportThisMethod Then
  362.             tempObjectID = RegisterObject.ObjectID
  363.         End If
  364.         
  365.         If tempObjectID = RegisterObject.ObjectID Then
  366.             If TypeName(tempEventObject.RegisterObject) = TypeName(RegisterObject) Then
  367.                 pvtObjectEvents.Remove I
  368.                 Set tempEventObject = Nothing
  369.                 I = I - 1
  370. '               Exit Function
  371.             End If
  372.         End If
  373.         
  374.         I = I + 1
  375.     Next tempEventObject
  376.  
  377.     UnRegisterForObjectEvent = True
  378.     Set tempEventObject = Nothing
  379. End Function
  380.  
  381.  
  382.  
  383.